<template>
  <el-form
    ref="userForm"
    :model="userInfo"
    :rules="rules"
    label-width="120px"
    style="margin-left: 120px; margin-top: 30px"
  >
    <el-form-item label="姓名:" prop="username">
      <el-input v-model="userInfo.username" style="width:300px" />
    </el-form-item>
    <el-form-item label="密码:" prop="password">
      <el-input v-model="userInfo.password" type="password" style="width:300px" />
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="hedit">更新</el-button>
    </el-form-item>
  </el-form>
</template>
<script>
import { userAvator } from '@/api/user'
import { saveUserDetailById } from '@/api/employees'
export default {
  data() {
    return {
      userInfo: {
        username: '',
        password: ''
      },
      rules: {
        username: [{ required: true, message: '用户名不能为空', trigger: 'blur' }],
        password: [{ required: true, message: '密码不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.loaduserAvator()
  },
  methods: {
    async loaduserAvator() {
      const res = await userAvator(this.$route.query.userid)
      // console.log(res)
      this.userInfo = { ...res.data, password: '' }
    },
    // 修改用户信息
    async doadd() {
      await saveUserDetailById(this.userInfo)
      // console.log(res)
      // 返回原来页面
      this.$router.back()
    },
    hedit() {
      // 兜底校验
      this.$refs.userForm.validate((valit) => {
        if (!valit) return // 未通过校验
        // 通过校验发请求
        this.doadd()
      })
    }
  }
}
</script>

